package com.supor.aiot.common.app.api;

import android.text.TextUtils;

import com.android.baseconfig.base.BaseApiResult;
import com.android.baseconfig.base.BaseRetrofit;
import com.android.baseconfig.base.DataApiResult;
import com.android.baseconfig.common.config.HttpConstant;
import com.android.baseconfig.common.utils.GsonUtil;
import com.android.baseconfig.common.utils.Logc;
import com.supor.aiot.common.entity.DictionaryBean;
import com.supor.aiot.common.entity.FeedbackBean;
import com.supor.aiot.common.entity.LaunchPageBean;
import com.supor.aiot.common.entity.QuestionTypeBean;
import com.supor.aiot.common.entity.UserAgreementBean;
import com.supor.aiot.common.entity.UserInfo;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.List;
import java.util.Map;

import io.reactivex.rxjava3.core.Observable;
import okhttp3.RequestBody;

/**
 * ---------------------------------------------------------------- <br>
 * Copyright (C) 2014-2020, by het, Shenzhen, All rights reserved.  <br>
 * ---------------------------------------------------------------- <br>
 * <p>
 * 描述: UserImpl <br>
 * 作者: lei <br>
 * 日期: 2020/11/7 <br>
 */
public class UserImpl extends BaseRetrofit<UserService> implements UserService {
    private static UserImpl instance;

    public static UserImpl getInstance() {
        if (instance == null) {
            instance = new UserImpl();
        }
        return instance;
    }

    @Override
    public Observable<BaseApiResult<String>> sendCaptcha(Map<String, String> map) {
        return api.sendCaptcha(map).compose(this.applySchedulers());
    }

    @Override
    public Observable<BaseApiResult<UserInfo>> login(String mobile, String CAPTCHA,
                                                     String providerId) {
        return api.login(mobile, CAPTCHA, providerId).compose(this.applySchedulers());
    }

    /**
     * 移动一键登录
     *
     * @param token        移动token
     * @return Observable<BaseApiResult>
     */
    public Observable<BaseApiResult<UserInfo>> cmccLogin(String token) {
        return api.cmccLogin("1", "0", "1", token).compose(this.applySchedulers());
    }

    /**
     * 移动一键登录
     *
     * @param operatorType 运营商类型:1:移动
     * @param providerId   默认0
     * @param type         手机平台：1-Android|2-iOS
     * @param token        移动token
     * @return Observable<BaseApiResult>
     */
    @Override
    public Observable<BaseApiResult<UserInfo>> cmccLogin(String operatorType, String providerId,
                                                       String type, String token) {
        return api.cmccLogin(operatorType, providerId, type, token).compose(this.applySchedulers());
    }

    @Override
    public Observable<BaseApiResult<String>> logout() {
        return api.logout().compose(this.applySchedulers());
    }

    @Override
    public Observable<BaseApiResult<String>> deleteUser(String bossUserId) {
        return api.deleteUser(bossUserId).compose(this.applySchedulers());
    }

    @Override
    public Observable<BaseApiResult<UserInfo>> getUser(String id) {
        return api.getUser(id).compose(this.applySchedulers());
    }

    public Observable<BaseApiResult<UserInfo>> updateUser(String id, Map<String, String> map) {
        RequestBody requestBody = RequestBody.create(HttpConstant.JSON,
                GsonUtil.getInstance().toJson(map));
        return updateUser(id, requestBody);
    }

    @Override
    public Observable<BaseApiResult<String>> getUserRank() {
        return api.getUserRank().compose(this.applySchedulers());
    }

    @Override
    public Observable<BaseApiResult<UserInfo>> updateUser(String id, RequestBody requestBody) {
        return api.updateUser(id, requestBody).compose(this.applySchedulers());
    }

    public Observable<BaseApiResult<DataApiResult<List<QuestionTypeBean>>>> getQuestionType() {
        return api.getQuestionType("QUESTION_TYPE").compose(this.applySchedulers());
    }

    @Override
    public Observable<BaseApiResult<DataApiResult<List<QuestionTypeBean>>>> getQuestionType(String sysCodeType) {
        return api.getQuestionType(sysCodeType).compose(this.applySchedulers());
    }

    @Override
    public Observable<BaseApiResult<DataApiResult<List<DictionaryBean>>>> getDictionary(String sysCodeType, int page, int limit) {
        return api.getDictionary(sysCodeType, page, limit).compose(this.applySchedulers());
    }

    /**
     * 反馈
     *
     * @param feedbackBean feedbackBean
     * @return BaseApiResult<String>
     */
    public Observable<BaseApiResult<String>> addFeedback(FeedbackBean feedbackBean) {
        return api.addFeedback(feedbackBean).compose(this.applySchedulers());
    }

    @Override
    public Observable<BaseApiResult<DataApiResult<List<UserAgreementBean>>>> getUserPrivacyProtocol() {
        return api.getUserPrivacyProtocol().compose(this.applySchedulers1());
    }

    @Override
    public Observable<BaseApiResult<DataApiResult<List<UserAgreementBean>>>> getUserServerProtocol() {
        return api.getUserServerProtocol().compose(this.applySchedulers1());
    }

    @Override
    public Observable<BaseApiResult<List<LaunchPageBean>>> getLaunchPageUrl() {
        return api.getLaunchPageUrl().compose(this.applySchedulers1());
    }

    @Override
    public Observable<BaseApiResult<List<LaunchPageBean>>> getLaunchPageUrl(Map<String, String> map) {
        return api.getLaunchPageUrl(map).compose(this.applySchedulers1());
    }

    @Override
    public Observable<BaseApiResult<String>> pushAppToken(RequestBody requestBody) {
        return api.pushAppToken(requestBody).compose(this.applySchedulers());
    }

    public Observable<BaseApiResult<String>> pushAppToken(String uniqueId) {
        if (TextUtils.isEmpty(uniqueId)) {
            Logc.e("pushAppToken: cant get uniqueId!");
            return null;
        }
        JSONObject jsonObject = new JSONObject();
        try {
            jsonObject.put("target", uniqueId);
            RequestBody requestBody = RequestBody.create(HttpConstant.JSON,
                    jsonObject.toString());
            return pushAppToken(requestBody);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return null;
    }

    public Observable<BaseApiResult<String>> pushAppToken(String mac, String uniqueId) {
        if (TextUtils.isEmpty(uniqueId)) {
            Logc.e("pushAppToken: cant get uniqueId!");
            return null;
        }
        JSONObject jsonObject = new JSONObject();
        try {
            jsonObject.put("mac", mac);
            jsonObject.put("target", uniqueId);
            RequestBody requestBody = RequestBody.create(HttpConstant.JSON,
                    jsonObject.toString());
            return pushAppToken(requestBody);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return null;
    }

}
